package com.zyy.smartcity1129.bus;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.BusLineInfo;
import com.zyy.smartcity1129.data.BusStopInfo;
import com.zyy.smartcity1129.util.RvAd;

import java.util.List;

/**
 * @ClassName: BusShowFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 5/12/2021$
 * @Version: 1.0
 */
public class BusShowFragment extends BaseFragment {
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private RecyclerView rv;
    private Button btnNext;
    private BusLineInfo.RowsBean busLine = BusFragment.line;
    private List<BusStopInfo.RowsBean> stop = BusFragment.stop;

    @Override
    protected int layId() {
        return R.layout.f_busshow;
    }

    @Override
    protected String title() {
        return "定制班车";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        BusFragment.liveData.observe(getViewLifecycleOwner(), m -> {
            remove();
        });
        tv1.setText("起点：" + busLine.getFirst());
        tv2.setText("终点：" + busLine.getEnd());
        tv3.setText("票价：" + busLine.getPrice());
        tv4.setText("里程：" + busLine.getMileage() + "km");
        RvAd<BusStopInfo.RowsBean> rvAd = new RvAd<BusStopInfo.RowsBean>(stop, R.layout.rv_text) {
            private void initView(View vi) {
                tv = (TextView) vi.findViewById(R.id.tv);
            }

            private TextView tv;

            @Override
            protected void initR(View vi, BusStopInfo.RowsBean rowsBean, int p) {
                initView(vi);
                if (p == 0) {
                    tv.setText("起点：" + rowsBean.getName());
                } else if (p == (stop.size() - 1)) {
                    tv.setText("终点：" + rowsBean.getName());
                } else {
                    tv.setText("\t\t\t\t\t" + rowsBean.getName());
                }
            }
        };
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.setAdapter(rvAd);
        btnNext.setOnClickListener(view -> {
            add(new BusDateFragment());
        });
    }

    private void initView(View iv) {
        tv1 = (TextView) iv.findViewById(R.id.tv1);
        tv2 = (TextView) iv.findViewById(R.id.tv2);
        tv3 = (TextView) iv.findViewById(R.id.tv3);
        tv4 = (TextView) iv.findViewById(R.id.tv4);
        rv = (RecyclerView) iv.findViewById(R.id.rv);
        btnNext = (Button) iv.findViewById(R.id.btnNext);
    }
}
